# Importing Data
Bahrain <- read_excel("C:/Users/Biniam/Desktop/Documents/Academic/Thesis/Analysis Folder/Excel Files/Bahrain.xlsx")

# Checking the Imported Data
View(Bahrain)
head(Bahrain)
tail(Bahrain)

# Creating Time Series Data
Bahrain_ts <- ts(Bahrain, start=2000, end=2018, frequency=12)

# Ploting the Time Series Data
plot(Bahrain_ts)

# Transformation of the Time Series Data incase of Seasonality and Random Fluctations
Bahrain_ts_log <- log(Bahrain_ts)
plot(Bahrain_ts_log)

# Decomposing the Time Series Data
install.packages("TTR")
library(TTR)

# Smoothing the Time Series Data
Bahrain_ts_sma3 <- SMA(Bahrain_ts,3)
plot(Bahrain_ts_sma3)

Bahrain_ts_sma6 <- SMA(Bahrain_ts,6)
plot(Bahrain_ts_sma6)

Bahrain_ts_sma12 <- SMA(Bahrain_ts,12)
plot(Bahrain_ts_sma12)

# Exponential Smoothing the Time Series Data
Bahrain_ts_ema1 <- EMA(Bahrain_ts,3, ratio=0.25)
plot(Bahrain_ts_ema1)

Bahrain_ts_ema2 <- EMA(Bahrain_ts,3, ratio=0.5)
plot(Bahrain_ts_ema2)

Bahrain_ts_ema3 <- EMA(Bahrain_ts,3, ratio=0.75)
plot(Bahrain_ts_ema3)

Bahrain_ts_ema4 <- EMA(Bahrain_ts,12, ratio=0.75)
plot(Bahrain_ts_ema4)

# Decomposing
Bahrain_ts_decomp <- decompose(Bahrain_ts)
plot(Bahrain_ts_decomp)

# Auto-Arima
Bahrain_ts
install.packages("forecast")
library(forecast)
Bahrain_ts_arima <- auto.arima(Bahrain_ts)
Bahrain_ts_arima

Bahrain_ts_forecast  <- forecast(Bahrain_ts_arima, h=240)
Bahrain_ts_forecast
plot(Bahrain_ts_forecast)

options(max.print=1000000)

accuracy(Bahrain_ts_forecast)

sum(is.na(Bahrain_ts))

summary(Bahrain_ts)

cycle(Bahrain_ts)

tail(Bahrain_ts)

tail(Bahrain)

Bahrain_ts1 <- ts(Bahrain, start= c(2000,1), end= c(2018,11), frequency=12)
Bahrain_ts1
cycle(Bahrain_ts1)
start(Bahrain_ts1)
end(Bahrain_ts1)
frequency(Bahrain_ts1)
head(Bahrain_ts1)
tail(Bahrain_ts1)
install.packages("forecast")
library(forecast)
Bahrain_ts1 <- tsclean(Bahrain_ts1)
Bahrain_ts1
plot(Bahrain_ts1)

plot(aggregate(Bahrain_ts1,FUN=mean))
#This will aggregate the cycles and display a year on year trend

install.packages("tseries")
library(tseries)
adf.test((Bahrain_ts1), alternative="stationary", k=0)
adf.test(diff(log(Bahrain_ts1)), alternative="stationary",k=0)

# To see any seasonal effect

boxplot(Bahrain_ts1~cycle(Bahrain_ts1))


# Decomposing
Bahrain_ts1_decomp <- decompose(Bahrain_ts1)
plot(Bahrain_ts1_decomp)

plot(Bahrain_ts1)
plot(log(Bahrain_ts1))
plot(diff(log(Bahrain_ts1)))

install.packages("tseries")
library(tseries)
acf(Bahrain_ts1)
acf(diff(log(Bahrain_ts1)))
acf(ts(diff(log10(Bahrain_ts1))))
pacf(Bahrain_ts1)
pacf(diff(log(Bahrain_ts1)))

# Observation
abline(reg=lm(Bahrain_ts1~time(Bahrain_ts1)))
Bahrain_ts1_model <- auto.arima(Bahrain_ts1, ic="aic", trace = TRUE)

# Model
# Best Model = model with the lowest aic value.
Bahrain_ts1_model <- auto.arima(Bahrain_ts1)
Bahrain_ts1_model
auto.arima(Bahrain_ts1, ic="aic", trace = TRUE)

# Indicates the stationarity of the model
library(tseries)
plot.ts(Bahrain_ts1_model$resid)
acf(ts(Bahrain_ts1)
acf(ts(Bahrain_ts1_model$residuals, main='ACF Residual')
pacf(ts(Bahrain_ts1_model$residuals, main='ACF Residual')

# Forecasting

library(forecast)
Bahrain_ts1_forecast <- forecast (Bahrain_ts1_model, level=c(95), h=10*12)
plot(Bahrain_ts1_forecast)
Bahrain_ts1_forecast

# Validating the model
Box.test(Bahrain_ts1_model$resid, lag=5, type="Ljung-Box")
Box.test(Bahrain_ts1_model$resid, lag=10, type="Ljung-Box")
Box.test(Bahrain_ts1_model$resid, lag=15, type="Ljung-Box")
